package com.sambo.inspection.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ListView;

import com.google.gson.JsonElement;
import com.sambo.inspection.R;
import com.sambo.inspection.activity.RoutingInspectionDetailActivity;
import com.sambo.inspection.adapter.CommonAdapter;
import com.sambo.inspection.adapter.TaskExpandableListAdapter;
import com.sambo.inspection.adapter.ViewHolder;
import com.sambo.inspection.bean.GlobleBean;
import com.sambo.inspection.bean.InspectionBean;
import com.sambo.inspection.http.HttpClient;
import com.sambo.inspection.http.HttpResponseHandler;
import com.sambo.inspection.http.Urls;
import com.sambo.inspection.util.JsonUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RoutingInspectionTaskFragment extends Fragment {

    ListView lvRecordList;

    private CommonAdapter<InspectionBean> mAdapter;
    private List<InspectionBean> mTaskParentList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_task, null);
        lvRecordList = contentView.findViewById(R.id.lv_task_list);

        mTaskParentList = new ArrayList<>();

        mAdapter = new CommonAdapter<InspectionBean>(getContext(), mTaskParentList, R.layout.item_task) {
            @Override
            public void convert(ViewHolder helper, InspectionBean item) {
                helper.setText(R.id.tv_task_code, item.getCode());
                helper.setText(R.id.tv_task_name, item.getName());
                if (TextUtils.equals("2", item.getState())) {
                    helper.setText(R.id.tv_state, "巡检中");
                } else {
                    helper.setText(R.id.tv_state, "未巡检");
                }
            }
        };

        lvRecordList.setAdapter(mAdapter);

        lvRecordList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getContext(), RoutingInspectionDetailActivity.class);
                intent.putExtra("title", "巡检任务");
                intent.putExtra("model", mTaskParentList.get(i));
                startActivity(intent);
            }
        });

        return contentView;
    }

    @Override
    public void onResume() {
        super.onResume();
        getInspectionRecord();
    }

    public void getInspectionRecord() {

        Map<String, String> params = new HashMap<>();
        params.put("userId", String.valueOf(GlobleBean.getInstance().getUserID()));
        params.put("executor", String.valueOf(GlobleBean.getInstance().getUserID()));
        params.put("page", "1");
        params.put("limit", "100");

        HttpClient.get(getContext(), Urls.API_SERVER_HOST + Urls.GET_TASK, params,
                new HttpResponseHandler() {

                    @Override
                    public void onInnovationSuccess(JsonElement value) {
                        super.onInnovationSuccess(value);
                        mTaskParentList.clear();

                        InspectionBean[] response = JsonUtil.toObj(value.toString(), InspectionBean[].class);

                        if (response != null && response.length > 0) {
                            for (InspectionBean bean : response) {
                                if (!TextUtils.equals("3", bean.getState())) {
                                    mTaskParentList.add(bean);
                                }
                            }
                        }

                        mAdapter.notifyDataSetChanged();

                    }
                }, null);
    }

}
